package com.cy.game

import android.content.Context
import com.business.Analyse
import com.business.AnalyseKey
import com.helloandroid.AppUtil
import com.helloandroid.R
import com.helloandroid.ads.VideoAdUtil
import com.helloandroid.base.BaseDialog
import com.helloandroid.tools.NetWorkUtil

class BigRedDialog(private val passLevel: Int, ctx: Context, val closeAction:(Boolean)->Unit) : BaseDialog(ctx) {

    override fun getContentViewResId(): Int = R.layout.yh_chengyu_red

    override fun initView() {
        findBtn(R.id.red_Button_close).setOnClickListener {
            cancel()
        }

        findBtn(R.id.red_Button_open).setOnClickListener { btnOpenClicked()}
    }

    private fun btnOpenClicked(){
        if (!NetWorkUtil.networkConnected()) {
            AppUtil.toast("请打卡网络!")
            return
        }

        // play video
        VideoAdUtil.playVideoAd(ctx as CyHomeActivity) { success ->
            if (success) {
                Analyse.report(AnalyseKey.Cy_PassLevel_Video)
                // 红包奖励
                giveAward()
                cancel()
            } else {
                AppUtil.toast("视频还没准备好!")
            }
        }
    }

    override fun initData() {
    }

    private fun giveAward() {
        var awardCoin = 0
        if (passLevel >= 50) {
            awardCoin = 88;
        } else {
            if (passLevel <= 5) {
                awardCoin = 58;
            } else if (passLevel <= 10) {
                awardCoin = 68;
            } else {
                awardCoin = 78;
            }
        }
        AppUtil.appViewModel().cyExtraAward(passLevel, awardCoin)
        AppUtil.toast("恭喜获得额外${awardCoin}金币奖励!")
        closeAction(true)
    }
}